package com.etlmaster.executor.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * xml文件解析器
 * 
 * @author Wugf
 * @date 2016-03-08
 */
public class XmlParser {
	public static List<Map<String,Object>> parseXMLUtils(String fileName,String nodeName){
		SAXReader saxReader = new SAXReader();

        Document document;
        List<Map<String,Object>> resultList=new ArrayList<Map<String,Object>>();
		try {
			document = saxReader.read(new File(fileName));
	        Element root = document.getRootElement();
	        List list=root.elements(nodeName);
	        for(Iterator it=list.iterator();it.hasNext();){
	        	Element element=(Element) it.next();
	        	Map<String,Object> map=getParamMap(element);
	        	resultList.add(map);
	        }
		} catch (DocumentException e) {
			
			e.printStackTrace();
		}
		return resultList;
		
	}
	private static Map<String, Object> getParamMap(Element rootElement) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		List<Element> paramsEs = (List<Element>) rootElement.selectNodes("./*");
		for (Element paramsE : paramsEs) {
			paramMap.put(paramsE.getName().trim(), paramsE.getStringValue());
		}
		return paramMap;
	}

}
